/*
 * This computer program is the confidential information and proprietary trade
 * secret of Apptonelabs, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * Apptonelabs, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of Apptonelabs, Inc.
 * 
 * Copyright (c) 2012-2015 Apptonelabs, Inc. All Rights Reserved.
 */
package com.apptonelabs.vnc.vphone;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

class ButtonPanel extends Panel implements ActionListener {

	VPhone viewer;
	Button disconnectButton;
	Button optionsButton;
	Button recordButton;
	Button clipboardButton;
	Button ctrlAltDelButton;
	Button refreshButton;
	Button enableVideoButton;
	Button selectButton;
	Button videoFreezeButton;

	final String videoOffLabel = "Video Off";
	final String videoOnLabel = "Video On";
	final String selectEnterLabel = "Select Video Area";
	final String selectLeaveLabel = "Hide Selection";
	final String videoFreezeLabel = " Freeze Video  ";
	final String videoUnfreezeLabel = "Unfreeze Video";

	ButtonPanel(VPhone v) {
		viewer = v;

		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		disconnectButton = new Button("Disconnect");
		disconnectButton.setEnabled(false);
		add(disconnectButton);
		disconnectButton.addActionListener(this);
		optionsButton = new Button("Options");
		add(optionsButton);
		optionsButton.addActionListener(this);
		clipboardButton = new Button("Clipboard");
		clipboardButton.setEnabled(false);
		add(clipboardButton);
		clipboardButton.addActionListener(this);
		if (viewer.rec != null) {
			recordButton = new Button("Record");
			add(recordButton);
			recordButton.addActionListener(this);
		}
		ctrlAltDelButton = new Button("Send Ctrl-Alt-Del");
		ctrlAltDelButton.setEnabled(false);
		add(ctrlAltDelButton);
		ctrlAltDelButton.addActionListener(this);
		refreshButton = new Button("Refresh");
		refreshButton.setEnabled(false);
		add(refreshButton);
		refreshButton.addActionListener(this);
	}

	/**
	 * Add video on/off button to the ButtonPanel.
	 */
	public void addVideoOffButton() {
		enableVideoButton = new Button(videoOffLabel);
		enableVideoButton.setEnabled(false);
		add(enableVideoButton);
		enableVideoButton.addActionListener(this);
	}

	/**
	 * Add video selection button to the ButtonPanel.
	 */
	public void addSelectButton() {
		selectButton = new Button(selectEnterLabel);
		selectButton.setEnabled(false);
		add(selectButton);
		selectButton.addActionListener(this);
	}

	/**
	 * Add video ignore button to the ButtonPanel.
	 */
	public void addVideoFreezeButton() {
		videoFreezeButton = new Button(videoFreezeLabel);
		videoFreezeButton.setEnabled(false);
		add(videoFreezeButton);
		videoFreezeButton.addActionListener(this);
	}

	//
	// Enable buttons on successful connection.
	//

	public void enableButtons() {
		disconnectButton.setEnabled(true);
		clipboardButton.setEnabled(true);
		refreshButton.setEnabled(true);
		if (enableVideoButton != null) {
			enableVideoButton.setEnabled(true);
		}
		if (selectButton != null) {
			selectButton.setEnabled(true);
		}
		if (videoFreezeButton != null) {
			videoFreezeButton.setEnabled(true);
		}
	}

	//
	// Disable all buttons on disconnect.
	//

	public void disableButtonsOnDisconnect() {
		remove(disconnectButton);
		disconnectButton = new Button("Hide desktop");
		disconnectButton.setEnabled(true);
		add(disconnectButton, 0);
		disconnectButton.addActionListener(this);

		optionsButton.setEnabled(false);
		clipboardButton.setEnabled(false);
		ctrlAltDelButton.setEnabled(false);
		refreshButton.setEnabled(false);
		if (enableVideoButton != null) {
			enableVideoButton.setEnabled(false);
		}
		if (selectButton != null) {
			selectButton.setEnabled(false);
		}
		if (videoFreezeButton != null) {
			videoFreezeButton.setEnabled(false);
		}
	}

	//
	// Enable/disable controls that should not be available in view-only
	// mode.
	//

	public void enableRemoteAccessControls(boolean enable) {
		ctrlAltDelButton.setEnabled(enable);
	}

	//
	// Event processing.
	//

	public void actionPerformed(ActionEvent evt) {

		viewer.moveFocusToDesktop();

		if (evt.getSource() == disconnectButton) {
			viewer.disconnect();

		} else if (evt.getSource() == optionsButton) {
			viewer.options.setVisible(!viewer.options.isVisible());

		} else if (evt.getSource() == recordButton) {
			viewer.rec.setVisible(!viewer.rec.isVisible());

		} else if (evt.getSource() == clipboardButton) {
			viewer.clipboard.setVisible(!viewer.clipboard.isVisible());

		} else if (evt.getSource() == ctrlAltDelButton) {
			try {
				final int modifiers = InputEvent.CTRL_MASK
						| InputEvent.ALT_MASK;

				KeyEvent ctrlAltDelEvent = new KeyEvent(this,
						KeyEvent.KEY_PRESSED, 0, modifiers, 127);
				viewer.rfb.writeKeyEvent(ctrlAltDelEvent);

				ctrlAltDelEvent = new KeyEvent(this, KeyEvent.KEY_RELEASED, 0,
						modifiers, 127);
				viewer.rfb.writeKeyEvent(ctrlAltDelEvent);

			} catch (IOException e) {
				e.printStackTrace();
			}

		} else if (evt.getSource() == refreshButton) {
			try {
				RfbProto rfb = viewer.rfb;
				rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
						rfb.framebufferHeight, false);
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else if (enableVideoButton != null
				&& evt.getSource() == enableVideoButton) {
			boolean enable = enableVideoButton.getLabel().equals(videoOnLabel);
			try {
				viewer.rfb.trySendVideoEnable(enable);
				enableVideoButton.setLabel(enable ? videoOffLabel
						: videoOnLabel);
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else if (selectButton != null && evt.getSource() == selectButton) {
			if (viewer.vc != null) {
				boolean isSelecting = viewer.vc.isInSelectionMode();
				if (!isSelecting) {
					selectButton.setLabel(selectLeaveLabel);
					viewer.vc.enableSelection(true);
				} else {
					selectButton.setLabel(selectEnterLabel);
					viewer.vc.enableSelection(false);
				}
			}

		} else if (videoFreezeButton != null
				&& evt.getSource() == videoFreezeButton) {
			boolean freeze = videoFreezeButton.getLabel().equals(
					videoFreezeLabel);
			try {
				viewer.rfb.trySendVideoFreeze(freeze);
				videoFreezeButton.setLabel(freeze ? videoUnfreezeLabel
						: videoFreezeLabel);
			} catch (IOException ex) {
				ex.printStackTrace();
			}

		}
	}
}
